﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using cde.UserControl;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using Cit.CDE.Net.Module;
using cde.cache;
using Cit.CDE.Net.Module.Commons;
using cde.SystemCommon;

public partial class main_comm_EditPointList : BasePage
{
    private const string EM_FN = "EM_FN";
    private const string TARGET_NAME = "TARGET_NAME";
    private const string TARGET_OBJ = "TARGET_OBJ";
    private const string CLM_SIZE = "CLM_SIZE";
    private const string CUR_NAME = "CUR_NAME";
    private const string ALL = "ALL";

    private CDEEntityManager moto_em;
    private List<string> attNames;
    private CDESearchCriteria sc;
    private DataTable dt;
    //private Hashtable attribute_value_name;
    private string curAttName;
    private string s_att;
    private string str_all;

    protected void Page_Load(object sender, EventArgs e)
    {
        string moto_EM_FN = Request.QueryString[EM_FN];
        moto_em = CDEFactory.getEMByFullName(moto_EM_FN);

        string targetAttName = Request.QueryString[TARGET_NAME];
        targetIds.Value = Request.QueryString[TARGET_OBJ];
        string[] names = targetAttName.Split(new char[] { ',' });

        curAttName = Request.QueryString[CUR_NAME];
        s_att = curAttName.Substring(0,curAttName.IndexOf("__"));

        str_all = Request.QueryString[ALL];

        attNames = new List<string>(names);

        sc = SearchCriteriaWithXml.CreateSC(moto_em, (Oprator)Session[CDEConstants.SESSION_LOIN_OPRATOR]);
        
        StringBuilder sbr = new StringBuilder();
        sbr.AppendFormat(Resources.Resource.ERR_MSG_OVERSTEP, sc.PageSize);
        lblWanring.Text = sbr.ToString();

        //attribute_value_name = new Hashtable();
        dt = (DataTable)Session["datatable"];
        addDgColumns(moto_em, attNames);

        if (!Page.IsPostBack)
        {
            btn_clear.Attributes.Add("onclick", "clear_openerValue();");

            SearchToTable();
            Session["datatable"] = dt;
        }

        InitConditions();
        dgmeisai.DataSource = dt;
        dgmeisai.DataBind();
        Page.DataBind();
    }

    private void SearchToTable()
    {
        XmlDocument doc = new XmlDocument();

        DataTable dst = CDEService.LoadEditPoints(moto_em, sc, curAttName);
        if (dst == null)
        {
            dt.Rows.Clear();
            lblResultCnt.Text = "0";
            return;
        }

        dt = dst;
        if (dt != null)
            lblResultCnt.Text = "" + dt.Rows.Count;

        foreach (DataRow dr in dt.Rows)
        {
            string values = EntityWithXml.getObject2String(dr[CDEConstants.CDE_DATAROW_ID]);
            string disps = values;
            for (int i = 1; i < attNames.Count; i++)
            {
                string clmName = attNames[i].Substring(s_att.Length + 2);
                values += "," + dr[clmName];
                disps += "," + this.getDisply(attNames[i], (string)dr[clmName]);
            }

            for (int i = 1; i < attNames.Count; i++)
            {
                string clmName = attNames[i].Substring(s_att.Length + 2);
                string disp_value = this.getDisply(attNames[i], (string)dr[clmName]);

                dr[clmName] = "<a href=\"JavaScript:set_opener_selectValue('" + values + "','" + disps + "')\">" + disp_value + "</a>";
            }
        }

    }

    private string getDisply(string attributeName, string value)
    {
        //if (attribute_value_name.Contains(attributeName))
        //{
        //    Hashtable value_name = (Hashtable)attribute_value_name[attributeName];
        //    if (value_name.Contains(value))
        //    {
        //        return (string)value_name[value];
        //    }
        //}
        CDEAttribute att = moto_em.getAttribute(attributeName);
        if (att.getP_Colmun().isHaveCandidate())
        {
            return att.getP_Colmun().getCandidateLable(value);
        }

        return value;
    }

    private void addDgColumns(CDEEntityManager em, List<string> attNames)
    {
        if (dt == null)
        {
            dt = new DataTable("entities");
        }

        foreach (string attributeName in attNames)
        {
            string label = "";
            string clm_type = "";
            CDEColumn pclm;
            string disp_type = "";

            CDEAttribute dispA = em.getAttribute(attributeName);
            if (dispA != null)
            {
                label = dispA.Label;
                pclm = dispA.getP_Colmun();
                disp_type = dispA.DISP_type;
            }
            else
            {
                CDECondition dispC = em.getCondition(attributeName);
                label = dispC.Label;
                pclm = dispC.getP_Colmun();
                disp_type = dispC.DISP_type;
            }
            clm_type = pclm.Type;

            if (CDEConstants.ConstDispType_NO_DISP.Equals(disp_type)
                || CDEConstants.ConstDispType_HIDDEN.Equals(disp_type)) continue;

            BoundColumn bc = new BoundColumn();
            string clmName = attributeName.Substring(s_att.Length + 2);

            bc.HeaderText = label + "<br /><input type=\"text\" name=\"" + clmName + "\" style=\"width:98%;\">";
            bc.DataField = clmName;
            if (!dt.Columns.Contains(clmName))
            {
                dt.Columns.Add(clmName);
            }

            if (clm_type == "S")
            {
                bc.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
            }
            else if (clm_type == "N")
            {
                bc.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
            }

            dgmeisai.Columns.Add(bc);

            //if (pclm.isHaveCandidate())
            //{
            //    List<CDECandidate> listCandidate = pclm.getCandidates();
            //    Hashtable value_name = new Hashtable();
            //    foreach (CDECandidate candidate in listCandidate)
            //    {
            //        value_name.Add(candidate.Value, candidate.Label);
            //    }
            //    attribute_value_name.Add(attributeName, value_name);
            //}
        }

    }

    protected void dgmeisai_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        dgmeisai.CurrentPageIndex = e.NewPageIndex;
        Page.DataBind();
    }


    protected void cmdSearch_Click(object sender, EventArgs e)
    {
        dgmeisai.CurrentPageIndex = 0;
        for (int i = 1; i < attNames.Count; i++)
        {
            string clmName = attNames[i].Substring(s_att.Length + 2);
            string value = Request.Form[clmName];
            if (!CDEConstants.IsEmpty(value))
            {
                sc.addCondition(clmName, "%" + value + "%", CDEConstCompare.Like);
            }
        }

        SearchToTable();
        dgmeisai.DataSource = dt;
        dgmeisai.DataBind();
        Session["datatable"] = dt;
    }

    private void InitConditions()
    {
        string clmName;
        string value;
        StringBuilder sb = new StringBuilder();

        sb.AppendLine("<script type=\"text/javascript\" language=\"javascript\">");
        for (int i = 1; i < attNames.Count; i++)
        {
            clmName = attNames[i].Substring(s_att.Length + 2);
            value = Request.Form[clmName] == null ? "" : Request.Form[clmName].ToString();
            sb.AppendLine("document.all." + clmName + ".value = '" + value + "';");
        }
        sb.AppendLine("</script>");
        Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sb.ToString());
    }
}
